//
//  EFFlashCell.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/2.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFFlashCell.h"
#import "EFSaleListItem.h"
#import "EFSepecialItem.h"
#import "EFTimeView.h"
#import "EFSaleView.h"
#import "EFFlashCell.h"
#import "EFTimeSegmentView.h"

@interface EFFlashCell ()
@property (strong, nonatomic) UIImageView *timeImageV;
@property (strong, nonatomic) UILabel *timeLabel;
@property (strong, nonatomic) UILabel *moreLabel;
@property (strong, nonatomic) UIImageView *arrowImageV;
@property (strong, nonatomic) UIView *topView;

@property (strong, nonatomic) EFTimeSegmentView *timeScrollView;
@property (strong, nonatomic) UIScrollView *singleContentScrollView;

@property (strong, nonatomic) UIView *timeTipView;
@property (strong, nonatomic) UILabel *rightLabel;
@property (strong, nonatomic) UILabel*leftLabel;

@property (strong, nonatomic) UIScrollView *contentScrollView;
@property (strong, nonatomic) UIImageView *selectImageView;
@property (strong, nonatomic) UILabel *dateSLabel;
@property (strong, nonatomic) UILabel *timeSLabel;

@end

@implementation EFFlashCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
        self.backgroundColor = RGBCOLOR(0xf5f9f9);
    }
    return self;
}

- (void)initUI{
    [self.contentView addSubview:self.topView];
    [self.topView addSubview:self.timeImageV];
    self.timeImageV.left = 18;
    self.timeImageV.top = 19;
    
    [self.topView addSubview:self.timeLabel];
    self.timeLabel.left = self.timeImageV.right + 5;
    self.timeLabel.top = self.timeImageV.top;
    
    [self.topView addSubview:self.arrowImageV];
    self.arrowImageV.top = 21;
    self.arrowImageV.right = kScreenWidth - 18;
    
    [self.topView addSubview:self.moreLabel];
    self.moreLabel.top = self.timeLabel.top;
    self.moreLabel.right = self.arrowImageV.left - 1;
    
    [self.contentView addSubview:self.timeScrollView];
    self.timeScrollView.top = self.topView.bottom;
    
    [self.contentView addSubview:self.timeTipView];
    [self.timeTipView addSubview:self.leftLabel];
    [self.timeTipView addSubview:self.rightLabel];
    self.timeTipView.top = self.timeScrollView.bottom;
    self.leftLabel.left = 20;
    self.leftLabel.top = (self.timeTipView.height - self.leftLabel.height)/2;
    self.rightLabel.right = kScreenWidth - 16;
    self.rightLabel.top = self.leftLabel.top;
    
    [self.contentView addSubview:self.contentScrollView];
    self.contentScrollView.top = self.timeTipView.bottom;
    
    [self.contentView addSubview:self.selectImageView];
    self.selectImageView.top = self.timeScrollView.top;
}

- (void)setObject:(NSMutableArray *)itemArray{
    __weak typeof(self) weakSelf = self;
     [self.timeScrollView setTimeSegmentItems:itemArray selectBlock:^(NSInteger index) {
         if (index <= itemArray.count) {
             //设置绿色图片跑到哪个文字
             //图片跑到点击的位置上
             weakSelf.selectImageView.left = index* 75;
             EFSaleListItem *listItem  = itemArray[index];
             NSArray *specialItem = listItem.productList;
             CGFloat x = 20;
             CGFloat y = 0;
             for (EFSepecialItem *item in specialItem) {
                 EFSaleView *view = [[EFSaleView alloc] initWithFrame:CGRectMake(x, y, 102, 170) Item:item];
                 
                 x += 102 + 5;
                 [weakSelf.contentScrollView addSubview:view];
             }
             weakSelf.contentScrollView.contentSize = CGSizeMake(20 + specialItem.count * 102 + (specialItem.count - 1)*5, 170);
             weakSelf.dateSLabel.text = listItem.date;
             [weakSelf.dateSLabel sizeToFit];
             weakSelf.timeSLabel.text = listItem.startTime;
             [weakSelf.timeSLabel sizeToFit];
         }
     }];
    //默认展示第一个
    EFSaleListItem *listItem  = itemArray[0];
    NSArray *specialItem = listItem.productList;
    CGFloat x = 20;
    CGFloat y = 0;
    for (EFSepecialItem *item in specialItem) {
        EFSaleView *view = [[EFSaleView alloc] initWithFrame:CGRectMake(x, y, 102, 170) Item:item];
        
        x += 102 + 5;
        [weakSelf.contentScrollView addSubview:view];
    }
    weakSelf.contentScrollView.contentSize = CGSizeMake(20 + specialItem.count * 102 + (specialItem.count - 1)*5, 170);

    //选中的label在和selectImageView保持一层才能看见
    weakSelf.selectImageView.left = 0;
    [self.selectImageView addSubview:self.dateSLabel];
    [self.selectImageView addSubview:self.timeSLabel];
    self.dateSLabel.text = listItem.date;
    [self.dateSLabel sizeToFit];
    self.dateSLabel.top= 3;
    self.dateSLabel.left = (self.selectImageView.width - self.dateSLabel.width)/2;
    
    self.timeSLabel.text = listItem.startTime;
    [self.timeSLabel sizeToFit];
    self.timeSLabel.top = self.dateSLabel.bottom + 5;
    self.timeSLabel.left = (self.selectImageView.width - self.timeSLabel.width)/2;

}

- (UIImageView *)timeImageV{
    if (!_timeImageV) {
        _timeImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
        _timeImageV.image = IMG(@"spiketitle_icon_clock_home");
    }
    return _timeImageV;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:13];
        _timeLabel.textColor = RGBCOLOR(0xc0b565);
        _timeLabel.text = @"怡食家限时秒杀";
        [_timeLabel sizeToFit];
    }
    return _timeLabel;
}

- (UILabel *)moreLabel{
    if (!_moreLabel) {
        _moreLabel = [[UILabel alloc] init];
        _moreLabel.font = [UIFont systemFontOfSize:13];
        _moreLabel.textColor = kBlack999;
        _moreLabel.text = @"更多";
        [_moreLabel sizeToFit];
    }
    return _moreLabel;
}

- (UIImageView *)arrowImageV{
    if (!_arrowImageV) {
        _arrowImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 11, 11)];
        _arrowImageV.image = IMG(@"textlink_icon_more_home");
    }
    return _arrowImageV;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 45)];
        [_topView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topG:)]];
        _topView.userInteractionEnabled = YES;
    }
    return _topView;
}

- (void)topG:(UITapGestureRecognizer *)tap{
    if (self.click) {
        self.click();
    }
}

- (EFTimeSegmentView *)timeScrollView{
    if (!_timeScrollView) {
        _timeScrollView = [[EFTimeSegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 45)];
    }
    return _timeScrollView;
}

- (UIView *)timeTipView{
    if (!_timeTipView) {
        _timeTipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 25)];
        _timeTipView.backgroundColor = RGBCOLOR(0xd2e9cc);
    }
    return _timeTipView;
}

- (UILabel *)rightLabel{
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.font = [UIFont systemFontOfSize:10];
        _rightLabel.textColor = RGBCOLOR(0x69ba45);
        _rightLabel.text = @"距离结束：";
        [_rightLabel sizeToFit];
    }
    return _rightLabel;
}

- (UILabel *)leftLabel{
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.font = [UIFont systemFontOfSize:10];
        _leftLabel.textColor = RGBCOLOR(0x69ba45);
        _leftLabel.text = @"正在抢购，先下单先得哦！";
        [_leftLabel sizeToFit];
    }
    return _leftLabel;
}

- (UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 170)];
        _contentScrollView.showsHorizontalScrollIndicator = false;
    }
    return _contentScrollView;
}

- (UIImageView *)selectImageView{
    if (!_selectImageView) {
        _selectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 51)];
        _selectImageView.image = IMG(@"Spike_menu_button_bg_home_selected");
    }
    return _selectImageView;
}

- (UILabel *)dateSLabel{
    if (!_dateSLabel) {
        _dateSLabel = [[UILabel alloc] init];
        _dateSLabel.font = [UIFont systemFontOfSize:13];
        _dateSLabel.textColor = [UIColor whiteColor];
    }
    return _dateSLabel;
}

- (UILabel *)timeSLabel{
    if (!_timeSLabel) {
        _timeSLabel = [[UILabel alloc] init];
        _timeSLabel.font = [UIFont boldSystemFontOfSize:17];
        _timeSLabel.textColor = [UIColor whiteColor];
    }
    return _timeSLabel;
}

@end
